package com.bop.dehong.center.transaction.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseFragment;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.center.adapter.ChargeRecordsAdapter;
import com.bop.dehong.center.transaction.TransactionActivity;
import com.bop.dehong.http.model.GetChargeRecordModel;
import com.bop.dehong.info.ChargeRecordsInfo;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

public class ChargeRecord extends BaseFragment implements OnHeaderRefreshListener, OnFooterRefreshListener {
	private ListView mListView;
	private ChargeRecordsAdapter mAdapter;
	private PullToRefreshView mRefreshView;
	private GetChargeRecordModel mGetRecordModel;
	private int mCurrentPage = 1;
	private int mTotalPage = 1;
	private int REQ_TYPE = 0;
	private final int REQ_REFRESH = 1;
	private final int REQ_LOAD = 2;
	private LinearLayout mEmptyLayout;
	private TransactionActivity mActivity;
	
	@Override
	protected void initTop() {
		mPageName = getClass().getName();
		
	}

	@Override
	public void onResume()
	{
		if (mActivity.isFristStart()) {
			initData();
			mActivity.setFristStart(false);
		}
		super.onResume();
	}
	
	@Override
	protected void initUI() {
		mEmptyLayout = getView(R.id.empty_layout);
		mListView = getView(R.id.transaction_list);
		mRefreshView = getView(R.id.transaction_list_pull_refresh_view);
		mRefreshView.setOnHeaderRefreshListener(this);
		mRefreshView.setOnFooterRefreshListener(this);
		if (mAdapter == null)
		{
			mAdapter = new ChargeRecordsAdapter(getActivity());
		}
		mListView.setAdapter(mAdapter);
	}

	@Override
	public void initData() {
		if (mAdapter != null && mAdapter.size() == 0){
			reqdata();
		}
	}

	private void reqdata() {
		showProgressDialog();
		if (mGetRecordModel == null) {
			mGetRecordModel = new GetChargeRecordModel(getActivity(), callback);
		}
		mGetRecordModel.doNet(DeHongApp.mLoginEntity.uid, mCurrentPage+"", "10");
	}

	RequestCallBack<ChargeRecordsInfo> callback = new RequestCallBack<ChargeRecordsInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(ChargeRecordsInfo info) {
			super.onSuccess(info);
			if (REQ_TYPE == REQ_REFRESH) {
				mAdapter.clear();
			}
			if (info.lists.size() > 0) {
				mEmptyLayout.setVisibility(View.GONE);
				mAdapter.appendToList(info.lists);
			}else {
				mEmptyLayout.setVisibility(View.VISIBLE);
			}
			mTotalPage = info.totalpages;
		};
		
	};

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mActivity = (TransactionActivity) activity;
	}	
	
	@Override
	protected int getContentViewId() {
		return R.layout.fragment_transactionrecord;
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view)
	{
		REQ_TYPE = REQ_REFRESH;
		mCurrentPage = 1;
		reqdata();
		mRefreshView.onHeaderRefreshComplete();
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view)
	{
		REQ_TYPE = REQ_LOAD;
		if (mCurrentPage < mTotalPage) {
			mCurrentPage ++;
			reqdata();
			mRefreshView.onFooterRefreshComplete();
		}else{
			ToastUtils.show(getActivity(), "没有数据可加载！");
			mRefreshView.onFooterRefreshComplete();
		}
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

//	@Override
//	public void uponClick(View v) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public void uponItemClick(AdapterView<?> arg0, View arg1, int arg2,
//			long arg3) {
//		// TODO Auto-generated method stub
//		
//	}
	
}
